package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;

import code.KeyStoreManager;
import code.Language;
import code.Settings;

import java.awt.Rectangle;
import java.io.File;
import java.util.HashMap;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class CertificateCA extends JPanel {

	Color c= new Color(0x80, 0x80, 0xFF);
	
	Color TAc = new Color(246, 250, 250);
	
	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel4 = null;

	private JLabel jLabel5 = null;

	private JLabel jLabel6 = null;

	ATOCOmain atocoframe;
	private KeyStoreManager ksm;

	private JLabel jLabel3 = null;

	private JLabel jLabel7 = null;

	private JLabel jLabel8 = null;

	private JLabel jLabel9 = null;

	private JScrollPane jScrollPane = null;

	private JTextArea jTextArea = null;

	private JLabel jLabel10 = null;
	
	private HashMap labels;
	
	private Settings s;

	/**
	 * This is the default constructor
	 */
	public CertificateCA(ATOCOmain atocoframe) {
		super();
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "CertificateCA", (String) s.global.get("installPath"));
		// Every Keystore operation must use this one.
		ksm = new KeyStoreManager(
				(String)Settings.global.get("caKeyStoreFile"), 
				(String)Settings.global.get("caKeyStorePassword"));
		if (ksm.getKeyStore() == null) {
        	System.out.println("Keystore file not found. Creating one.");
        	ksm.createKeyStore(
        			(String)Settings.global.get("caKeyStoreFile"), 
    				(String)Settings.global.get("caKeyStorePassword"));
        }
		this.atocoframe=atocoframe;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel10 = new JLabel();
		jLabel10.setBounds(new Rectangle(595, 85, 75, 16));
		jLabel10.setText((String) labels.get("jLabel10"));
		jLabel10.setFont(new java.awt.Font("Arial", Font.BOLD, 10));
		jLabel10.setForeground(c);
		jLabel9 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel9.setBounds(new Rectangle(195, 334, 25, 25));
		jLabel9.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				jTextArea.setText((String) labels.get("revoke"));
			}
		});
		jLabel8 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel8.setBounds(new Rectangle(194, 241, 25, 25));
		jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				jTextArea.setText((String) labels.get("view"));
			}
		});
		jLabel7 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel7.setBounds(new Rectangle(194, 150, 25, 25));
		jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				jTextArea.setText((String) labels.get("add"));
			}
		});
		jLabel3 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
		jLabel3.setBounds(new Rectangle(67, 42, 25, 25));
		jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				jTextArea.setText((String) labels.get("addself"));
			}
		});
		jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/rev_cert.gif"));
		jLabel6.setBounds(new Rectangle(223, 303, 210, 89));
		jLabel6.setText((String) labels.get("jLabel6"));
		//jLabel6.setForeground(c);
		jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
			System.out.println("action performed");
			}
		});
	
		jLabel5 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/listandview_cert.gif"));
		jLabel5.setBounds(new Rectangle(223, 208, 247, 89));
		jLabel5.setText((String) labels.get("jLabel5"));
		//jLabel5.setForeground(c);
		jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				atocoframe.contentPane(new CAListCertificate(atocoframe));
			}
		});
	
		jLabel4 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/create_empty_cert.gif"));
		jLabel4.setBounds(new Rectangle(223, 109, 211, 97));
		jLabel4.setText((String) labels.get("jLabel4"));
		//jLabel4.setForeground(c);
		jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				atocoframe.contentPane(new CACreateCertificate(atocoframe));
			}
		});
		jLabel2 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/encdec3.jpg"));
		jLabel2.setBounds(new Rectangle(444, 40, 40, 43));
		jLabel2.setText("");
		if(ksm.hasCaCertificate()) {
			jLabel2.setVisible(false);
		}
		
		jLabel1 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/sign_cert.gif"));
		jLabel1.setBounds(new Rectangle(103, 14, 334, 91));
		if(ksm.hasCaCertificate()) {
			jLabel1.setText("Delete Self-Signed Certificate");
		} else {
			jLabel1.setText((String) labels.get("selfsigned"));
		}
		//jLabel1.setForeground(c);
		jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				if(ksm.hasCaCertificate()) {
					ksm.deleteCert("cert-cacert");
					ksm.deleteKeyPair("key-cacert");
					ksm.saveKeyStore();
					System.err.println("Ca cert is deleted.");
				} else {
					atocoframe.contentPane(new CACreateSelfSignedCert(atocoframe));
				}
			}
		});
		jLabel1.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 15));
		jLabel = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/stop2.gif"));
		jLabel.setBounds(new Rectangle(23, 37, 41, 38));
		jLabel.setText("");
		if(!ksm.hasCaCertificate()) {
			jLabel.setVisible(false);
		}
		
		this.setSize(825, 420);
		this.setLayout(null);
		this.add(jLabel, null);
		this.setBackground(Color.WHITE);
		this.add(jLabel1, null);
		this.add(jLabel2, null);

		this.add(jLabel4, null);
		this.add(jLabel5, null);
		this.add(jLabel6, null);
		this.add(jLabel3, null);
		this.add(jLabel7, null);
		this.add(jLabel8, null);
		this.add(jLabel9, null);
		this.add(getJScrollPane(), null);
		this.add(jLabel10, null);
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(510, 105, 250, 286));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBackground(TAc);
			jTextArea.setLineWrap(true);
			jTextArea.setWrapStyleWord(true);
		}
		return jTextArea;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
